const sass = require('sass')
const loadGruntTasks = require('load-grunt-tasks')

module.exports = grunt => {
  grunt.initConfig({
    // clean: {
    //   temp:'dist'
    // },
    sass: {
      options: {
        // 指定grunt-sass中用哪个处理sass
        implementation: sass
      },
      main: {
        files: {
          'dist/assets/styles/main.css': 'src/assets/styles/main.scss'
        }
      }
    },
    babel: {
      options: {
        presets: ['@babel/preset-env']
      },
      main: {
        files: {
          'temp/assets/scripts/main.js': 'src/assets/scripts/main.js'
        }
      }
    },
    page: {
      main {
        files: {
          'temp/assets/scripts/main.js': 'src/assets/scripts/main.js'
        }
      }
    },
    watch: {
      js: {
        files: ['src/assets/scripts/*.js'],
        tasks: ['babel']
      },
      css: {
        files: ['src/assets/styles/*.scss'],
        tasks: ['sass']
      }
    },
    uglify: {
      main: {
        files: {
          'dist/assets/scripts/main.js': 'temp/assets/scripts/main.js'
        }
      }
    },
    copy: {
      main: {
        files: {
          expand: true, 
          src: ['public/*'], 
          dest: 'dist/', 
        },
      }
    }
  })

  // 自动加载多个插件
  // loadGruntTasks('grunt')

  grunt.loadNpmTasks('grunt-babel')
  grunt.loadNpmTasks('grunt-sass')
  grunt.loadNpmTasks('grunt-contrib-uglify')
  grunt.loadNpmTasks('grunt-contrib-watch')
  grunt.loadNpmTasks('grunt-contrib-copy')

  // 脚本文件的build任务
  grunt.registerTask('babel-build', ['babel', 'uglify'])

  grunt.registerTask('default', ['sass', 'babel', 'watch'])
}